package com.digital.community.common.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

public class HttpDownloader {
    private Context c;
	public HttpDownloader(Context c) {
		this.c = c;
	}
	
    public int downloadFile(String urlStr, String path, String fileName) {   
        InputStream inputStream = null;   
        try {
        	FileUtils fileUtils = new FileUtils();
        	if(fileUtils.isFileExist(path, fileName)) {
        		fileUtils.DeleteFile(path, fileName);
        	}
        	
        	URL url = new URL(urlStr);
        	HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
        	inputStream = urlConn.getInputStream(); 
        	
        	File resultFile = fileUtils.Write2FileFromInput(path, fileName, inputStream);
        	if(resultFile==null) {
        		return -1;
        	}
        }
        catch (Exception e) {
        	System.out.println(e);
            return -1;
        }finally {
        	if(inputStream != null) {
        		try {inputStream.close();} catch (IOException e) {}
        	}
        }
        return 0;   
    }
       
    
    public int downloadFileToSDCard(String fileName) {
    	InputStream inputStream = null;
    	try {
        	String path = null;
        	boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        	if(sdCardExist) {
        		path = Environment.getExternalStorageDirectory().toString() + "/";
        	} else {
        		return -2;
        	}
        	
        	FileUtils fileUtils = new FileUtils();
        	if(fileUtils.isFileExist(path, fileName)) {
        		fileUtils.DeleteFile(path, fileName);
        	}
        	System.out.println("path: " + path);
        	System.out.println("fileName: " + fileName);
        	URL url = new URL(getURL() + "?fileName=" + fileName);
        	HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
        	inputStream = urlConn.getInputStream(); 
        	
        	File resultFile = fileUtils.Write2FileFromInput(path, fileName, inputStream);
        	if(resultFile==null) {
        		return -1;
        	}
    	} catch (Exception e) {
    		return -1;
    	} finally {
    		if(inputStream != null) {
    			try {inputStream.close();} catch (IOException e) {}
    		}
    	}
    	
    	return 0;
    }
    
    private static final String DEFAULT_URL = HttpUtils.DEFAULT_URL;
	private String getURL() {
		String urlPrefix = DEFAULT_URL;
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(c);
		String ipUserSet = sp.getString("ip", "");
		if(StringUtils.isNotBlank(ipUserSet)) {
			urlPrefix = ipUserSet;
		}
		
		
		return urlPrefix + "download.do";
	}
}
